var win = Ti.UI.currentWindow;
var tabGroup = win.tabGroup;

var numberOfOffers = 30;
var SolrCommDest = 'All_Beach_Destinations';

//// create star selection

var starview = Titanium.UI.createView({
	top:30,
	left:30,
	right:30,
	height:40,
	backgroundImage:'imgs/4star_sized.png'
	
});

var stars = 2;

starview.addEventListener('click',function(e){
	
	//alert(e.x);
	Ti.API.info('starclick value =' + e.x);
	if (e.x >= 1 && e.x <= 52)
	{
		starview.backgroundImage = 'imgs/1star_sized.png';
		if (stars == 1){starview.backgroundImage = 'imgs/1star_sized.png'}
		stars = 1; 
		Ti.API.info(stars);
	}
	if (e.x >= 53 && e.x <= 105)
	{
		starview.backgroundImage = 'imgs/2star_sized.png';
		if (stars == 2){
			starview.backgroundImage = 'imgs/1star_sized.png';
			stars = 1;}
		else{stars = 2}
		Ti.API.info(stars);
	}
	else if (e.x >= 106 && e.x <= 157)
	{
		starview.backgroundImage = 'imgs/3star_sized.png';
		if (stars == 3){
			starview.backgroundImage = 'imgs/2star_sized.png';
			stars = 2;}
		else{stars = 3}
		Ti.API.info(stars);
	}
	else if (e.x >= 158 && e.x <= 208)
	{
		starview.backgroundImage = 'imgs/4star_sized.png';
		if (stars == 4){
			starview.backgroundImage = 'imgs/3star_sized.png';
			stars = 3;}
		else{stars = 4}
		Ti.API.info(stars);
	}
	else if (e.x >= 209 && e.x <= 260)
	{
		starview.backgroundImage = 'imgs/5star_sized.png';
		if (stars == 5){
			starview.backgroundImage = 'imgs/4star_sized.png';
			stars = 4;}
		else{stars = 5}
		//Ti.API.info('old'+stars);
	
		Ti.API.info(stars);
	}	
	})

Ti.API.info(stars);

win.add(starview);

var budgetImg = Ti.UI.createImageView({
	image: 'imgs/budget_gbp.png'
	
});



var budgetview = Ti.UI.createScrollView({
	contentWidth:'auto', 
	//contentHeight:'auto', 
	height:30,
	top:80, 
	left:0,
	right:0,
	showHorizontalScrollIndicator:true
	
}) 

budgetview.scrollTo(215,0);

budgetview.addEventListener('scroll', function(e)
{
	Ti.API.info('x ' + e.x + ' y ' + e.y);

});

budgetview.add(budgetImg);
win.add(budgetview);

var budgetarrow = Ti.UI.createImageView({
	image: 'imgs/arrow_up.png',
	top: 110
})

win.add(budgetarrow);

var starlabel = Ti.UI.createLabel({
		text:'Minimum Hotel Rating...' ,
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:15
	},
		left:10,
		top:5
	
})

win.add(starlabel);
/////create elements for scrollable view

var view1 = Ti.UI.createImageView({
	backgroundColor:'blue',
	image: 'imgs/egypt-sized.jpg',
	clickName:'image',
	width:320
});
view1l = Ti.UI.createLabel({
	text:'Egypt' ,
	solrCommDest:'Egypt_-_Any', 
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:25
	},
		right:20,
		bottom:10
	
	
})
view1.add(view1l);

var view2 = Ti.UI.createImageView({
	image: 'imgs/majorca_sized.jpg',
	clickName:'image',
	width:320
});

view2l = Ti.UI.createLabel({
		text:'Spain' ,
		solrCommDest:'Spain_-_Any',
	color:'#2f4f4f',
	font:{
		fontFamily:'Verdana',
		fontSize:25
	},
		right:20,
		bottom:10
	
	
})
view2.add(view2l);
var view3 = Ti.UI.createImageView({
	image: 'imgs/turkey_sized.jpg',
	clickName:'image',
	width:320
});
view3l = Ti.UI.createLabel({
	text:'Turkey' ,
	solrCommDest:'Turkey_-_Any',
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:25
	},
	right:20,
	bottom:10
	
})
view3.add(view3l);

var view4 = Ti.UI.createView({
	backgroundColor:'#23238e'
});

view4l = Ti.UI.createLabel({
	text:'All Beach Destinations' ,
	solrCommDest:'All_Beach_Destinations',
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:25
		},
	right:20,
	bottom:10	
})
view4.add(view4l);

var view5 = Ti.UI.createView({
	backgroundColor:'black'
});

view5l = Ti.UI.createLabel({
		text:'Any Destinations' ,
		color:'#fff',
		font:{
			fontFamily:'Verdana',
			fontSize:25
		},
		right:20,
		bottom:10	
})
view5.add(view5l);

var view6 = Ti.UI.createView({
	backgroundColor:'blue'
});
var view7 = Ti.UI.createImageView({
	image: 'imgs/london_small.jpg',
	clickName:'image',
	width:320});
	
view7l = Ti.UI.createLabel({
		text:'All London',
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:15
	},
		left:150,
		bottom:5
	});

view7.add(view7l);	
	
var view8 = Ti.UI.createView({
	backgroundColor:'yellow'
});

destlabel = Ti.UI.createLabel({
		text:'Swipe your Destination...' ,
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:15
	},
		left:10,
		top:10
	
})

aptlabel = Ti.UI.createLabel({
		text:'Airport...' ,
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:15
	},
		left:10,
		top:10
	
})

//// adding the views above to a scrollableView

var scrollViewDest = Titanium.UI.createScrollableView({
	views:[view1,view2,view3,view4,view5],
	pagingControlColor:'#13386c',
	showPagingControl:true,
	pagingControlHeight:15,
	maxZoomScale:5.0,
    zoomScale:3.0,
    minZoomScale:1.0,
	currentPage:3
});

var scrollViewApt = Titanium.UI.createScrollableView({
	views:[view6,view7,view8],
	pagingControlColor:'#13386c',
	showPagingControl:true,
	pagingControlHeight:15,
	maxZoomScale:2.0,
	currentPage:1
});

//// create search button

var searchButton = Titanium.UI.createImageView({
    image: 'imgs/searchbutton_320.png',
	//clickName:'image',
	});

var searchContainer = Titanium.UI.createView({
	left:5,
	right:5,
	//width:320,
	top:360,
	//backgroundColor:'#ff0099'
});

var actInd = Titanium.UI.createActivityIndicator({
	bottom:19, 
	left:20,
	height:15,
	width:15,
	style:Titanium.UI.iPhone.ActivityIndicatorStyle.BIG,

});


searchText = Ti.UI.createLabel({
	text:'Show my Holidays' ,
	color:'#fff',
	font:{
		fontFamily:'Verdana',
		fontSize:20
		}
})


searchContainer.add(searchButton);
searchContainer.add(searchText);

/// create a view that will contain the scrollableView

var searchviewDest = Titanium.UI.createView({
   //borderRadius:10,
   //backgroundColor:'#e0e0e0',
//   width:50,
   left:0,
   right:0,
   height:130,
   top:235
});
var searchviewApt = Titanium.UI.createView({
   //borderRadius:10,
   //backgroundColor:'#e0e0e0',
   width:160,
   left:0,
   height:100,
   top:135
});

//// add elements to win

win.add(searchviewDest);

win.hideNavBar();
win.add(searchviewApt);
searchviewApt.add(scrollViewApt);
searchviewDest.add(scrollViewDest);
searchviewDest.add(destlabel);
searchviewApt.add(aptlabel);
win.add(searchContainer);
win.add(actInd);





var xhr = Titanium.Network.createHTTPClient({
	onload: function(e) {
       
		Ti.App.fireEvent('gotResults', {resultsText: this.responseText});  
		actInd.hide();	
	},
	
	onerror: function(e) {
        Ti.API.debug(e.error);
        win.setToolbar(null,{animated:true});
        alert('Please try again');
        actInd.hide();
    },
    timeout:10000
});

scrollViewDest.addEventListener('scroll', function(e)
{
    switch(e.currentPage)
{
case 0:
  SolrCommDest = view1l.solrCommDest;
  break;
case 1:
  SolrCommDest = view2l.solrCommDest;
  break;
case 2:
  SolrCommDest = view3l.solrCommDest;
  break;
case 3:
  SolrCommDest = view4l.solrCommDest;
break;
default:
  SolrCommDest = 'ANY';
}
Titanium.API.info("Destination: " + SolrCommDest);
});

searchButton.addEventListener('click', function()
{

	//Ti.API.info(surl);
	var url = "http://50.19.87.190/mtu/solr2json2.php?offers="+numberOfOffers+"&stars="+stars+"&commdest="+SolrCommDest;
    Titanium.API.info("URL sent:"+url);
	xhr.open("GET", url);
	actInd.show();
	xhr.send();
	Ti.API.info('hide actind');
});


scrollViewApt.addEventListener('scroll', function(e)
{
    Titanium.API.info("Airport Image Scrolled current page: " + e.currentPage);
}); 

